package com.xayy.health.mgt.service.impl;

import cn.kawins.base.result.CheckResult;
import cn.kawins.base.utils.CollUtil;
import cn.kawins.mybatis.base.BaseServiceImpl;
import com.xayy.health.mgt.mapper.BatchUploadDataMapper;
import com.xayy.health.mgt.mapper.BatchUploadDataRecordMapper;
import com.xayy.health.mgt.model.BatchUploadData;
import com.xayy.health.mgt.model.BatchUploadDataRecord;
import com.xayy.health.mgt.model.dto.DataFeatureDto;
import com.xayy.health.mgt.service.BatchUploadDataService;
import com.xayy.health.mgt.service.DataQualityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangxa
 */
@Service
public class BatchUploadDataServiceImpl extends BaseServiceImpl<BatchUploadData> implements BatchUploadDataService {

    @Autowired
    private BatchUploadDataMapper batchUploadDataMapper;

    @Autowired
    private BatchUploadDataRecordMapper batchUploadDataRecordMapper;

    @Autowired
    private DataQualityService dataQualityService;

    /**
     * 上传excel
     *
     * @param batchUploadDataList 批量上传数据
     */
    @Override
    public void save(List<BatchUploadData> batchUploadDataList) {
        if (CollUtil.isNotEmpty(batchUploadDataList)) {
            BatchUploadDataRecord record = BatchUploadDataRecord.builder()
                    .fileName(batchUploadDataList.get(0).getFileName())
                    .pictureNum(batchUploadDataList.get(0).getPictureNum())
                    .dataTotal(batchUploadDataList.get(0).getDataTotal())
                    .NumberOfCurves(batchUploadDataList.get(0).getNumberOfCurves())
                    .satelliteName(batchUploadDataList.get(0).getSatelliteName())
                    .satelliteCode(batchUploadDataList.get(0).getSatelliteCode())
                    .createTime(new Date())
                    .fileLogo(batchUploadDataList.get(0).getFileLogo())
                    .isDelete(1)
                    .build();
            batchUploadDataMapper.insertUploadDataRecord(record);

            batchUploadDataList.forEach(item -> {
                item.setCreateTime(new Date());
                item.setIsDelete(1);
            });
        }
        batchUploadDataMapper.insertList(batchUploadDataList);
    }

    /**
     * 获取上传记录信息
     * @param satelliteName 卫星名称
     * @return CheckResult<Object>
     */
    @Override
    public CheckResult<Object> getUploadList(String satelliteName) {
        return CheckResult.success(batchUploadDataRecordMapper.select(BatchUploadDataRecord.builder().isDelete(1).satelliteName(satelliteName).build()));
    }

    /**
     * 根据卫星名称文件标识查询图例参数
     * @param fileLogo 文件标识
     * @param satelliteName 卫星名称
     * @return CheckResult<Object>
     */
    @Override
    public CheckResult<Object> getLegendNameList(Long fileLogo, String satelliteName) {
        ArrayList<Object> list = new ArrayList<>();
        List<BatchUploadData> currentExcelList = select(BatchUploadData.builder().fileLogo(fileLogo).isDelete(1).satelliteName(satelliteName).build());
        if (CollUtil.isNotEmpty(currentExcelList)) {
            Map<String, List<BatchUploadData>> legendNameList = currentExcelList.stream().collect(Collectors.groupingBy(BatchUploadData::getLegendName));
            Set<String> keySet = legendNameList.keySet();
            for (String key : keySet) {
                List<BatchUploadData> dataList = legendNameList.get(key);
                String[] array = dataList.stream().map(BatchUploadData::getParamCode).toArray(String[]::new);
                for (BatchUploadData data : dataList) {
                    data.setParamCodes(array);
                    list.add(data);
                    break;
                }
            }
            return CheckResult.success(list);
        } else {
            return CheckResult.fail("查询图例名称无数据，请确认excel中的卫星名称和所选卫星名称是否一致");
        }
    }

    /**
     * 批量特征分析图表
     * @param dataFeatureDto 特征分析实体
     * @return CheckResult<Object>
     */
    @Override
    public CheckResult<Object> getBatchFeatureEcharts(DataFeatureDto dataFeatureDto) {
        List<String> paramCodesList = dataFeatureDto.getParamCodes();
        if (CollUtil.isNotEmpty(paramCodesList)) {
            ArrayList<Object> finalList = new ArrayList<>();
            for (String param : paramCodesList) {
                dataFeatureDto.setParamCode(param);
                CheckResult<Object> featureEcharts = dataQualityService.getFeatureEcharts(dataFeatureDto);
                Object data = featureEcharts.getData();
                finalList.add(data);
            }
            return CheckResult.success(finalList);
        } else {
            log.info("前端传递的参数代号为空");
            return CheckResult.success();
        }
    }

    /**
     * 根据图例名称查询参数代号
     * @param fileLogo 文件标识
     * @param satelliteName 卫星名称
     * @param legendName 图例名称
     * @return CheckResult<Object>
     */
    @Override
    public CheckResult<Object> getParamCodesListByLegendName(Long fileLogo, String satelliteName, String legendName) {
        List<BatchUploadData> dataList = batchUploadDataMapper.select(BatchUploadData.builder().fileLogo(fileLogo).satelliteName(satelliteName).legendName(legendName).build());
        if (CollUtil.isNotEmpty(dataList)) {
            List<String> collect = dataList.stream().map(BatchUploadData::getParamCode).collect(Collectors.toList());
            return CheckResult.success(collect);
        } else {
            return CheckResult.success("查询结果为空");
        }

    }

    /**
     * 删除上传记录
     * @param fileLogo 文件标识
     * @return CheckResult<Object>
     */
    @Override
    public CheckResult<Object> deleteBatchDataByFileLogo(Long fileLogo) {
        batchUploadDataMapper.updateStatus(fileLogo);
        batchUploadDataMapper.updateBatchUploadRecordStatus(fileLogo);
        return CheckResult.success("删除成功");
    }
}


